package com.zuilapsa.jmc.exporters;

import org.dom4j.Element;

import com.zuilapsa.jmc.*;
import com.zuilapsa.jmc.model.*;
import com.zuilapsa.jmc.userInterfaces.UserInterfaceSession;

import java.io.*;

public class NetworkExporter implements ExporterInterface {

	public void export(UserInterfaceSession interfaceSession) throws JmcException {
		exportDomainname();
		exportHostname();
	}

	public void exportDomainname() throws JmcException {
		File outputFile = new File(Level
				.getPropertyValue("/config/jmc/rootdir")
				+ "/etc/conf.d/domainname");
		Configuration.getInstance();
		// .getConfigProperty("config.jmc.rootdir")
		// + "/etc/conf.d/domainname");
		// TODO fix ME!!!!!!!!!!1
		try {
			PrintStream out = new PrintStream(outputFile);
			out.println("OVERRIDE=1");
			out.println("DNSDOMAIN=\""
					+ Level.getPropertyValue("/config/network/domainname")
					+ "\"");
			out.close();
		} catch (FileNotFoundException e) {
			throw new JmcException("File not found: ", e);
		}
	}

	public void exportHostname() throws JmcException {
		File outputFile = new File(Level
				.getPropertyValue("/config/jmc/rootdir")
				+ "/etc/conf.d/hostname");
		// Configuration.getInstance();
		// .getConfigProperty("config.jmc.rootdir")
		// + "/etc/conf.d/hostname");
		try {
			PrintStream out = new PrintStream(outputFile);
			out
					.println("HOSTNAME=\""
							+ Level
									.getPropertyValue("/config/network/hostname")
							+ "\"");
			out.close();
		} catch (FileNotFoundException e) {
			throw new JmcException("File not found: ", e);
		}
	}

	public void exportNet(Element configElement) throws JmcException {
		File outputFile = new File(Level
				.getPropertyValue("/config/jmc/rootdir")
				+ "/etc/conf.d/net");

		// Configuration.getInstance();
		// .getConfigProperty("config.jmc.rootdir")
		// + "/etc/conf.d/net");
		try {
			PrintStream out = new PrintStream(outputFile);
			out.println("config_eth0=("
					+ Level.getPropertyValue("/config/network/if/eth0") + "\"");
			out.close();
		} catch (FileNotFoundException e) {
			throw new JmcException("File not found: ", e);
		}
	}
}
